<?php
/**
 * класс предназначен для отслеживания нагрузки
 * @author 
 * @version 1.1
 */
class Pressure {
	public static $start = null;
	public static $stop = null;

	/**
	 * Устанавливает начальное время выполнения скрипт
	 */
	public static function setStart() {
		list($usec, $sec) = explode(" ", microtime());
		self::$start = (float)$usec + (float)$sec;
	}

	/**
	 * Устанавливает конечное время выполнения скрипт
	 */
	public static function setStop() {
		list($usec, $sec) = explode(" ", microtime());
		self::$stop = (float)$usec + (float)$sec;
	}
	/**
	 * Метод возвращает текущее время выполнения скрипта (разница)
	 *
	 * @return integer
	 */
	public static function calculateCurrentDuration() {
		self::setStop();
		return self::$stop - self::$start;
	}
	/**
	 * Метод сохраняет данные по нагрузке в БД
	 *
	 * @param string $url
	 * @param integer $type
	 * @param float $duration
	 * @return boolean - status of operation
	 */
	public static function saveDuration($url, $type, $duration) {
		$objDB = Service_Database::instance();

		$stmtFields = "`".DB_PREFIX."pressure`.`pressure_type`, `".DB_PREFIX."pressure`.`duration`, `".DB_PREFIX."pressure`.`url`, `".DB_PREFIX."pressure`.`url_small`, `".DB_PREFIX."pressure`.`creation`";
		$stmtValues = "'" . $type . "', '" . $duration . "', '" . $url . "', '" . getCurrentSmallUrl() . "', '" . Service_Date::gLocalTime2BD() . "'";

		$objSession = User_Session::instance();
	  $userId = $objSession->getUserId();
	  if($userId) {
	    $stmtFields .= ", `".DB_PREFIX."pressure`.`id_user`";
	  	$stmtValues .= ", '" . $userId . "'";
	  }
		$stmt = "INSERT INTO `".DB_PREFIX."pressure`	(" . $stmtFields . ") VALUES (" . $stmtValues . ")";

		return $objDB->query($stmt);
	}
	public static function getPressure($listType) {
		$objDB = Service_Database::instance();
		switch ($listType) {
			case 0:
			  $stmt= "SELECT AVG(`duration`) AS duration, COUNT(`url` ) AS `amount`, `url`, `pressure_type`
				FROM `".DB_PREFIX."pressure`
				WHERE  `creation` > '2010-03-27 23:33:00'
				GROUP BY `url`, `pressure_type`
				ORDER BY 1 DESC
				LIMIT 0, 75";
			  break;
			case 1:
			  $stmt= "SELECT AVG(`duration`) AS duration, COUNT(`url_small` ) AS `amount`, `url_small`, `pressure_type`
				FROM `".DB_PREFIX."pressure`
				WHERE `url_small` != '' AND `creation` > '2010-03-27 23:33:00'
				GROUP BY `url_small`, `pressure_type`
				ORDER BY 1 DESC
				LIMIT 0, 75";
			  break;
			case 2:
			  $stmt= "SELECT SUM(`duration`) AS duration, COUNT(`url_small` ) AS `amount`, `url_small`, `pressure_type`
				FROM `".DB_PREFIX."pressure`
				WHERE `url_small` != '' AND `creation` > '2010-03-27 23:33:00'
				GROUP BY `url_small`, `pressure_type`
				ORDER BY 1 DESC
				LIMIT 0, 75";
			  break;
			default:
			  $stmt= "SELECT AVG(`duration`) AS duration, COUNT(`url` ) AS `amount`, `url`, `pressure_type`
				FROM `".DB_PREFIX."pressure`
				WHERE `creation` > '2010-03-27 23:33:00'
				GROUP BY `url`, `pressure_type`
				ORDER BY 1 DESC
				LIMIT 0, 75";
			  break;
		}
		return $objDB->select($stmt);
	}
}
?>